package com.hxkj.controller.article;

import com.hxkj.domain.po.Resources;
import com.hxkj.domain.po.ResourcesTree;
import com.hxkj.entity.enums.FilePathEnum;
import com.hxkj.service.FileService;
import com.hxkj.service.article.ResourcesService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 资源控制层
 * <p>
 * Created by yaosy on 2018年6月11日
 */
@RestController
@RequestMapping("/resources")
public class ResourcesController {


    @Autowired
    private ResourcesService resourcesService;

    @Autowired
    private FileService fileService;

    /**
     * 新增资源
     *
     * @param resources
     * @throws Exception
     */
    @RequestMapping(value = "/saveResources")
    public Callable<Resources> saveResources(@ModelAttribute Resources resources) throws Exception {
        if(resources.getIsDirectory()==1 || resources.getIsDirectory() ==2){
            return ()-> resourcesService.saveResources(resources);
        }else{
            return ()-> resourcesService.saveResourcesFolder(resources);
        }

    }

    /**
     * 新建目录资源
     *
     * @param resources
     * @throws Exception
     */
    @RequestMapping(value = "/saveResourcesFolder")
    public Callable<Resources> saveResourcesFolder(@ModelAttribute Resources resources) throws Exception {
        return ()-> resourcesService.saveResourcesFolder(resources);
    }

    /**
     * 获取模板功能树
     *
     * @param resources
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getResourcesTree")
    public Callable<List<ResourcesTree>> getResourcesTree(@ModelAttribute Resources resources) throws Exception {
        return () ->resourcesService.getResourcesTree(resources);
    }


    /**
     *删除资源
     *
     * @param resources
     * @throws Exception
     */
    @RequestMapping(value = "/remove")
    public Callable<ResultMessageStruct> remove(@ModelAttribute Resources resources) throws Exception{
        return () -> resourcesService.remove(resources);
    }

    /**
     * 获取单条资源数据
     *
     * @param resources
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get")
    public Callable<ResultDataStruct<Resources>> get(@ModelAttribute Resources resources) throws Exception {
        return () ->resourcesService.get(resources);
    }

    /**
     * 批量资源上传
     *
     * @param file 图片信息   第二个参数为自定义一个 存放文件的目录名称
     * @throws Exception
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Callable<List<String>> imgUpload(@RequestParam(value = "file", required = false) MultipartFile[] file) throws Exception {
        return () -> fileService.templetUploud(file, FilePathEnum.RESOURCES_FILE_PATH.getDescription());
    }


    /**
     * 保存单条资源文件
     *
     * @param resources
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify")
    public Callable<ResultMessageStruct> modify(@ModelAttribute Resources resources) throws Exception {
        return () ->resourcesService.modify(resources);
    }

    /***
     * 上传文件后保存到数据库当中
     *
     * @param templet
     * @throws Exception
     * ***/
    @RequestMapping(value = "/saveUploadContent")
    public Callable<Resources> saveUploadContent(@ModelAttribute Resources templet) throws Exception {
        return ()-> resourcesService.saveUploadContent(templet);
    }

    /****
     * 根据站点下载资源文件
     * ****/
    @RequestMapping(value = "/download")
    public void download(HttpServletRequest request, HttpServletResponse response , @ModelAttribute Resources resources) throws Exception {
        resourcesService.download(request , response, resources);
    }
}
